//
//  SearchViewController.m
//  NoneCar
//
//  Created by DongSen on 2018/7/17.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "TaskTableViewCell.h"
#import "SearchViewController.h"

@interface SearchViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UITextField *plateTextfield;
@property(nonatomic,strong)NSMutableArray * dataArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topToView;


@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BackGroundColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"TaskTableViewCell" bundle:nil] forCellReuseIdentifier:@"TaskTableViewCell"];
    [self createNavBar];
    // Do any additional setup after loading the view from its nib.
}
-(void)createNavBar{
    UIView *navBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, kNAVIGATIONBARHEIGHT)];
    navBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navBar];
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(MARGIN, kSTATUSBARHEIGGT+8, SCREEN_W-66-MARGIN, 28)];
    titleView.backgroundColor =RGBAColor(241,241,241,1);
    titleView.layer.cornerRadius = 4;
    titleView.layer.masksToBounds = YES;
    [navBar addSubview:titleView];
    UIImageView *searchIcon = [[UIImageView alloc] init];
    searchIcon.image = [UIImage imageNamed:@"小搜索"];
    [titleView addSubview:searchIcon];
    [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleView).offset(8);
        make.centerY.mas_equalTo(titleView);
        make.height.mas_equalTo(16);
        make.width.mas_equalTo(16);
    }];
    self.plateTextfield = [[UITextField alloc] init];
    [titleView addSubview:self.plateTextfield];
    self.plateTextfield.placeholder = @"被保险人/报案号/案件编号/保单号";
    self.plateTextfield.font = Font(14);
    self.plateTextfield.delegate = self;
    self.plateTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.plateTextfield.returnKeyType = UIReturnKeySearch;
    [self.plateTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleView).offset(32);
        make.right.mas_equalTo(titleView);
        make.centerY.mas_equalTo(titleView);
        make.height.mas_equalTo(28);
    }];
    UIButton *cancelbtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [cancelbtn setTitle:@"取消" forState:(UIControlStateNormal)];
    cancelbtn.titleLabel.font = Font(17);
    [cancelbtn setTitleColor:APPColor forState:(UIControlStateNormal)];
    [cancelbtn addTarget:self action:@selector(searchButton) forControlEvents:(UIControlEventTouchUpInside)];
    [navBar addSubview:cancelbtn];
    [cancelbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(navBar).offset(0);
        make.centerY.mas_equalTo(titleView);
        make.height.mas_equalTo(28);
        make.width.mas_equalTo(60);
    }];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, kNAVIGATIONBARHEIGHT-0.5, SCREEN_W, 0.5)];
    line.backgroundColor = BackGroundColor;
    [navBar addSubview:line];
    self.topToView.constant = kNAVIGATIONBARHEIGHT;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self.plateTextfield becomeFirstResponder];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.plateTextfield resignFirstResponder];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self searchNetWork];
}
-(void)searchNetWork{
    [self showLoading];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (int i =0; i<1; i++) {
            NSString * tests = [NSString stringWithFormat:@"asda%d",i];
            [self.dataArray addObject:tests];;
        }
        [self hidloading];
        if (self.dataArray.count) {
            self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        }
        [self.tableView reloadData];
    });
}
- (void)searchButton{
    [self pop];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.dataArray.count){
        [self.blackView removeFromSuperview];
    }
    else{
        [self.tableView bringSubviewToFront:self.blackView];
    }
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TaskTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TaskTableViewCell"];
    //    cell.opinionLabel.text =[NSString stringWithFormat:@"%@",self.dataArray[indexPath.section]];
    return cell;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
